package com.xbongbong.saas.enums.business;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.saas.constant.PaymentConstant;
import com.xbongbong.saas.enums.SetTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.base.PaymentSheetBaseEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 回款单枚举
 *
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/14 15:37
 * @since v1.0
 */
@Getter
public enum PaymentSheetEnum {

    //TODO 有改动的枚举：
    // 1.CONTRACT LINK_BUSINESS_SINGLE改为LINK_BUSINESS_MULTI
    // 2.PAYMENT  LINK_BUSINESS_SINGLE改为LINK_BUSINESS_MULTI
    // 3.BELONG_ID text_2改为array_2 USER改为USER_GROUP
    // 4.RED_SHEET num_4改为text_18   num改为 LINK_BUSINESS_SINGLE
    // 新增的枚举：
    // 1.PAYMENT_SHEET_TYPE 回款单类型
    // 2.REFUND 关联退货
    // 3.WRITE_OFF_AMOUNT 核销金额
    // 4.INVOICE_AMOUNT 开票金额
    // 5.FUND_ACCOUNT 收款账户/红冲资金账户
    // 注意敲代码的时候格式的改变导致的影响记得改动  -zwq

    // other_1：基础信息
    BASE_INFO(PaymentSheetBaseEnum.BASE_INFO.getAttr(), "enum.paymentSheet.attrName.base_info", "baseInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 10000, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // serialNo：编号
    SHEET_NO(PaymentSheetBaseEnum.SHEET_NO.getAttr(), "enum.paymentSheet.attrName.sheet_no", "sheetNo", FieldTypeEnum.SERIALNO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9990, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // text_10：类型，回、付款单字段attr都统一用PaymentConstant.ATTR_SHEET_TYPE，即 text_10
    PAYMENT_SHEET_TYPE(PaymentSheetBaseEnum.PAYMENT_SHEET_TYPE.getAttr(), "enum.paymentSheet.attrName.payment_sheet_type", "paymentSheetType", FieldTypeEnum.COMBO.getType(), 1, 0, 0, ShowTypeEnum.LIST_DETAIL.getCode(), 9980, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // text_6：关联客户
    CUSTOMER_ID(PaymentSheetBaseEnum.CUSTOMER_ID.getAttr(), "enum.paymentSheet.attrName.customer_id", "customerId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9970, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // text_4：关联合同
    CONTRACT(PaymentSheetBaseEnum.CONTRACT.getAttr(), "enum.paymentSheet.attrName.contract", "contract", FieldTypeEnum.LINK_BUSINESS_MULTI.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9960, 1, 1, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    // text_5：关联应收款
    PAYMENT(PaymentSheetBaseEnum.PAYMENT.getAttr(), "enum.paymentSheet.attrName.payment", "payment", FieldTypeEnum.LINK_BUSINESS_MULTI.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    // num_1：回款金额
    AMOUNT(PaymentSheetBaseEnum.AMOUNT.getAttr(), "enum.paymentSheet.attrName.amount", "amount", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // num_7：核销金额
    WRITE_OFF_AMOUNT(PaymentSheetBaseEnum.WRITE_OFF_AMOUNT.getAttr(), "enum.paymentSheet.attrName.write_off_amount", "writeOffAmount", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.LIST_DETAIL.getCode(), 9930, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // num_8：开票金额
    INVOICE_AMOUNT(PaymentSheetBaseEnum.INVOICE_AMOUNT.getAttr(), "enum.paymentSheet.attrName.invoice_amount", "invoiceAmount", FieldTypeEnum.NUM.getType(), 1, 1, 0, ShowTypeEnum.LIST_DETAIL.getCode(), 9920, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // array_1：归属人
    BELONG_ID(PaymentSheetBaseEnum.BELONG_ID.getAttr(), "enum.paymentSheet.attrName.belong_id", "belongId", FieldTypeEnum.USER_GROUP.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9910, 1, 1, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    // date_1：回款日期
    PAYMENT_TIME(PaymentSheetBaseEnum.PAYMENT_TIME.getAttr(), "enum.paymentSheet.attrName.payment_time", "paymentTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9900, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // text_14：收款账户
    FUND_ACCOUNT(PaymentSheetBaseEnum.FUND_ACCOUNT.getAttr(), "enum.paymentSheet.attrName.fund_account", "fundAccount", FieldTypeEnum.COMBO.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9890, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // text_3：支付方式
    PAY_METHOD(PaymentSheetBaseEnum.PAY_METHOD.getAttr(), "enum.paymentSheet.attrName.pay_method", "payMethod", FieldTypeEnum.COMBO.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9880, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // other_2：扩展信息
    SYSTEM_INFO(PaymentSheetBaseEnum.SYSTEM_INFO.getAttr(), "enum.paymentSheet.attrName.system_info", "systemInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9870, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // text_81：完成回款计划 - 为防止用户自定义字段占用，该字段不删除
    PAYMENT_TASK(PaymentSheetBaseEnum.PAYMENT_TASK.getAttr(), "enum.paymentSheet.attrName.payment_task", "paymentTask", FieldTypeEnum.LINK_BUSINESS_MULTI.getType(), 3, 0, 1, ShowTypeEnum.NONE.getCode(), 9865, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // text_7：备注
    MEMO(PaymentSheetBaseEnum.MEMO.getAttr(), "enum.paymentSheet.attrName.memo", "memo", FieldTypeEnum.TEXTAREA.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9860, 1, 0, ScreenTypeEnum.SCREEN_SHOW.getType()),
    // file_1：附件
    FILE(PaymentSheetBaseEnum.FILE.getAttr(), "enum.paymentSheet.attrName.file", "file", FieldTypeEnum.UPLOAD.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9850, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // text_16：关联原始回款单
    RED_SHEET(PaymentSheetBaseEnum.RED_SHEET.getAttr(), "enum.paymentSheet.attrName.red_sheet", "redSheet", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // creatorId：创建人
    CREATOR_ID(PaymentSheetBaseEnum.CREATOR_ID.getAttr(), "enum.paymentSheet.attrName.creator_id", "creatorId", FieldTypeEnum.CREATORID.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9820, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // addTime：创建时间
    ADD_TIME(PaymentSheetBaseEnum.ADD_TIME.getAttr(), "enum.paymentSheet.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9810, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // updateTime：更新时间
    UPDATE_TIME(PaymentSheetBaseEnum.UPDATE_TIME.getAttr(), "enum.paymentSheet.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9800, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // ownerId：负责人
    OWNER_ID(PaymentSheetBaseEnum.OWNER_ID.getAttr(), "enum.paymentSheet.attrName.owner_id", "ownerId", FieldTypeEnum.OWNERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9790, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    // coUserId：协同人
    COUSERID(PaymentSheetBaseEnum.COUSERID.getAttr(), "enum.paymentSheet.attrName.couser_id", "coUserId", FieldTypeEnum.COUSERID.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9780, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     sysLong10：
     系统级别字段，与data平级。因子表单改造后，系统字段各种类型预占位了一部分，后续业务开发新增的系统级别字段都统一使用占位的字段
     当前sysLong10，代表生成规则，默认值为1;1:手动创建应收 2:按开票产生应收 3:按出库产生应收
     */
    SYS_LONG_10(PaymentSheetBaseEnum.SYS_LONG_10.getAttr(), "enum.paymentSheet.attrName.sys_long_10", "sysLong10", FieldTypeEnum.SYS_LONG.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 8000, 0, 0, ScreenTypeEnum.SCREEN_OFF.getType()),

    // text_11：关联客户隐藏
    CUSTOMER_NAME_LINK_TEXT(PaymentSheetBaseEnum.CUSTOMER_NAME_LINK_TEXT.getAttr(), "enum.paymentSheet.attrName.customer_name_link_text", "customerNameLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9790, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    // text_12：关联合同隐藏
    CONTRACT_LINK_TEXT(PaymentSheetBaseEnum.CONTRACT_LINK_TEXT.getAttr(), "enum.paymentSheet.attrName.contract_link_text", "contractLinkText", FieldTypeEnum.TEXTAREA.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9780, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    // text_13：关联应收款隐藏
    PAYMENT_LINK_TEXT(PaymentSheetBaseEnum.PAYMENT_LINK_TEXT.getAttr(), "enum.paymentSheet.attrName.payment_link_text", "paymentLinkText", FieldTypeEnum.TEXTAREA.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9770, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    // text_15：收款账户隐藏/红冲资金账户隐藏
    FUND_ACCOUNT_LINK_TEXT(PaymentSheetBaseEnum.FUND_ACCOUNT_LINK_TEXT.getAttr(), "enum.paymentSheet.attrName.fund_account_link_text", "fundAccountLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9760, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    // text_17：关联原始回款单隐藏
    RED_SHEET_LINK_TEXT(PaymentSheetBaseEnum.RED_SHEET_LINK_TEXT.getAttr(), "enum.paymentSheet.attrName.red_sheet_link_text", "redSheetLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9750, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    // text_82：关联回款计划隐藏
    PAYMENT_TASK_LINK_TEXT(PaymentSheetBaseEnum.PAYMENT_TASK_LINK_TEXT.getAttr(), "enum.paymentSheet.attrName.payment_task_link_text", "paymentTaskLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9750, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),

    //TODO 以下字段回款单改造时需要删掉，后续开发到一定程度即可删除
    REF_TYPE("num_5", "enum.paymentSheet.attrName.ref_type", "refType", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9899, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    PAYMENT_SHEET_NO("text_1", "enum.paymentSheet.attrName.payment_sheet_no", "paymentSheetNo", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9990, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    ESTIMATE_TIME("date_2", "enum.paymentSheet.attrName.estimate_time", "estimateTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9950, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    IS_BAD("num_2", "enum.paymentSheet.attrName.is_bad", "isBad", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9910, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    IS_RED("num_3", "enum.paymentSheet.attrName.is_red", "isRed", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9900, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    ;
    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 详见
     *
     * @see ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;
    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    PaymentSheetEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable,
                     Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }

    /**
     * 对data内的字段拼上“data.”
     * @param cache
     * @return java.lang.String
     * @author zcp
     * @date 2019/3/1 18:12
     * @since v1.0
     * @version v1.0
     */
    public static String getAttrConnectData(PaymentSheetEnum cache) {
        return "data." + cache.getAttr();
    }
    /**
     * 获取es查询、聚合字段
     * data在库内是json对象，对data字段内某个key做es【精确查询】时，需要在其前拼上“data.”，且【text等类型的】要在后面拼“.keyword”
     * @param cache 当前枚举对象
     * @return java.lang.String 处理后的聚合字段
     * @author zcp
     * @date 2019/2/21 14:59
     * @since v1.0
     * @version v1.0
     */
    public static String getEsAttr4Keyword(PaymentSheetEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.fieldType);
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }

    /**
     * 流程中获取隐藏字段
     * @return
     */
    public static List<PaymentSheetEnum> getDisableField() {
        List<PaymentSheetEnum> paymentSheetEnums = new ArrayList<>();
        for (PaymentSheetEnum paymentSheetEnum : values()) {
            if (Objects.equals(paymentSheetEnum.getShowType(), ShowTypeEnum.NONE.getCode()) || Objects.equals(paymentSheetEnum.getEditHide(), 1)
                    || Objects.equals(paymentSheetEnum.getFieldType(), FieldTypeEnum.CREATORID.getType()) || Objects.equals(paymentSheetEnum.getFieldType(), FieldTypeEnum.ADDTIME.getType())) {
                paymentSheetEnums.add(paymentSheetEnum);
            }
        }
        return  paymentSheetEnums;
    }
    /**
     * app端默认标题显示字段
     *
     * @return java.util.List<java.lang.Object>
     * @author 徐俊杰
     * @date 2019/11/20 15:22
     * @since v1.0
     */
    public static List<Object> getTitleAttrList() {
        return Collections.singletonList(SHEET_NO);
    }

    /**
     * app端默认摘要
     *
     * @return java.util.List<java.lang.Object>
     * @author 徐俊杰
     * @date 2019/11/20 15:24
     * @since v1.0
     */
    public static List<Object> getSummaryAttrList() {
        return Arrays.asList(CUSTOMER_ID, CONTRACT, AMOUNT, PAYMENT_TIME, BELONG_ID);
    }

    /**
     * 得到所有的字段attr
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @date 19/11/26 026 10:25
     * @update 19/11/26 026 10:25
     * @since v1.0
     * @version v1.0
     */
    public static List<String> getAllAttr() {
        List<String> allAttr = new ArrayList<>();
        for (PaymentSheetEnum cache : values()) {
            allAttr.add(cache.attr);
        }
        return allAttr;
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    public String getI18nAttrName() {
        return attrName;
    }

    public static String getI18nValue(String value) {
        return I18nMessageUtil.getMessage(value);
    }

    public static List<String> getAttrs() {
        List<String> attrs = new ArrayList<>();
        for (PaymentSheetEnum paymentSheetEnum : values()) {
            attrs.add(paymentSheetEnum.getAttr());
        }
        return attrs;
    }
}
